﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System
Partial Public Class Service
#Region "销售单_取基础资料列表"
    Public Function _BILL_Product_GetBaseList(ByVal FSession As String, ByVal FBType As Integer, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Product_GetBaseList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            Select Case FStyle
                Case "*"
                    sqlstr = "select * from Base_Product where FType=" & FBType
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = FControl.GetErrString("读取基础资料失败!", ErrString)
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "销售单_增加/更改基础资料"
    Public Function _BILL_Product_ManageBase(ByVal FSession As String, ByRef FCinf As FBaseProduct, ByRef ErrString As String) As Boolean Implements IService._BILL_Product_ManageBase
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCinf Is Nothing Then
                ErrString = "参数错误"
                Return False
            End If
            If FCinf.id = 0 Then
                ErrString = "参数错误"
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            If FCinf.id > 0 Then
                '--更新
                sqlstr = String.Format("update Base_Product set FName='{0}',FCName='{1}',FUnit='{2}',FPrice='{3}'where id={4}", FCinf.FName, FCinf.FCName, FCinf.FUnit, FCinf.FPrice, FCinf.id)
                Return DControl.ExecSQL(sqlstr, ErrString)
            ElseIf FCinf.id = -1 Then
                '--增加
                If FCinf.FType <> 0 And FCinf.FType <> 1 Then
                    ErrString = "参数错误"
                    Return False
                End If
                sqlstr = String.Format("select * from Base_Product where FName='{0}' and FType={1}", FCinf.FName, FCinf.FType)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables(0).Rows.Count > 0 Then
                    ErrString = "项目已经存在"
                    Return False
                End If

                sqlstr = String.Format("insert into Base_Product(FName,FCName,FUnit,FPrice,FType) values('{0}','{1}','{2}','{3}','{4}')", FCinf.FName, FCinf.FCName, FCinf.FUnit, FCinf.FPrice, FCinf.FType)

                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    Return False
                End If
                sqlstr = String.Format("select id from Base_Product where FName='{0}' and FType={1}", FCinf.FName, FCinf.FType)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = "未知错误"
                    Return False
                End If
                FCinf.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            Else
                ErrString = "参数错误"
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "销售单_获取基础资料信息"
    Public Function _BILL_Product_GetBaseInfo(ByVal FSession As String, ByRef FCinf As FBaseProduct, ByRef ErrString As String) As Boolean Implements IService._BILL_Product_GetBaseInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCinf Is Nothing Then
                ErrString = "参数错误"
                Return False
            End If
            If FCinf.id <= 0 Then
                ErrString = "参数错误"
                Return False
            End If
            If FCinf.FType < 0 Or FCinf.FType > 1 Then
                ErrString = "参数错误"
                Return False
            End If
            '--取资料
            Dim sqlstr As String = "select * from Base_Product where id=" & FCinf.id
            Dim ds As DataSet = Nothing
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "未找到指定项目"
                Return False
            End If
            Dim t As DataRow = ds.Tables(0).Rows(0)
            FCinf.FName = t.Item("FName")
            FCinf.FCName = t.Item("FCName")
            FCinf.FPrice = t.Item("FPrice")
            FCinf.FType = t.Item("FType")
            FCinf.FUnit = t.Item("FUnit")
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "销售单_保存"
    Public Function _BILL_Product_SaveBill(ByVal FSession As String, ByRef FBInf As FBillProduct, ByRef ErrString As String) As Boolean Implements IService._BILL_Product_SaveBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBInf Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If

            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            If FBInf.id > 0 Then
                '--检查审核权限
                If FBInf.FUid = FUinf.id Then
                    '--自己的单据
                    If _User_CheckAuth(FSession, "BILL", "FUBILLVERIFYMY", ErrString) = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                Else
                    '--其它人的单据
                    If _User_CheckAuth(FSession, "BILL", "FUBILLVERIFY", ErrString) = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                End If
            Else
                '--新单据
                If _User_CheckAuth(FSession, "BILL", "FUBILLNEW", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            End If
            '--保存单据
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FBInf.id > 0 Then
                '--检查分组
                Dim FUids As String() = Nothing
                If GetUserPair(FUinf.id, FUids, ErrString) = False Then
                    FBInf.FVid = Nothing
                    FBInf.FVName = Nothing
                    Return False
                End If
                If FUids.Contains(FBInf.FUid) = False Then
                    ErrString = "与制单人不在同一组,无法执行收费操作!"
                    FBInf.FVid = Nothing
                    FBInf.FVName = Nothing
                    Return False
                End If
                '--审核
                sqlstr = String.Format("update Bill_PRoduct set FVid={0},FVName='{1}' where id={2}", FUinf.id, FUinf.FRName, FBInf.id)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = String.Format("单据收费失败!{0}错误信息:{1}", vbCrLf, ErrString)
                    Return False
                End If
                Return True
            Else
                '--保存
                '--判断数据
                Dim FCinfo As FBaseProduct = Nothing
                '--判断客户
                If FBInf.FCid = 0 Then
                    ErrString = "客户信息不正确"
                    Return False
                End If
                'If FBinf.FCid = -1 Then
                '    FCinfo = New FBaseProduct With {.FName = FBInf.FName, .FCName = FBInf.FCName}
                '    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                '        Return False
                '    End If
                '    If FCinfo.id = Nothing Then
                '        ErrString = "增加新客户失败!"
                '        Return False
                '    End If
                '    FBinf.FCid = FCinfo.id
                'Else
                '    '--更新原有客户
                '    _BILL_Prop_ModifyCustom(FSession, New FBaseProperty With {.id = FBinf.FCid, .FName = FBinf.FName, .FCName = FBinf.FCName}, ErrString)
                'End If
                FCinfo = New FBaseProduct With {.id = FBInf.FCid, .FType = 0, .FName = FBInf.FName, .FCName = FBInf.FCName}
                If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                    Return False
                End If
                '--判断分录
                '-第一行
                If Not FBInf.FItemName1 Is Nothing Then
                    '--有数据
                    If FBInf.FItem1 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--项目需要新增
                    FCinfo = New FBaseProduct With {.id = FBInf.FItem1, .FName = FBInf.FItemName1, .FPrice = FBInf.FPrice1, .FType = 1, .FUnit = FBInf.FUnit1}
                    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBInf.FItem1 = FCinfo.id
                End If
                '-第二行
                If Not FBInf.FItemName2 Is Nothing Then
                    '--有数据
                    If FBInf.FItem2 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--项目需要新增
                    FCinfo = New FBaseProduct With {.id = FBInf.FItem2, .FName = FBInf.FItemName2, .FPrice = FBInf.FPrice2, .FType = 1, .FUnit = FBInf.FUnit2}
                    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBInf.FItem2 = FCinfo.id
                End If

                '-第三行
                If Not FBInf.FItemName3 Is Nothing Then
                    '--有数据
                    If FBInf.FItem3 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--项目需要新增
                    FCinfo = New FBaseProduct With {.id = FBInf.FItem3, .FName = FBInf.FItemName3, .FPrice = FBInf.FPrice3, .FType = 1, .FUnit = FBInf.FUnit3}
                    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBInf.FItem3 = FCinfo.id
                End If

                '-第四行
                If Not FBInf.FItemName4 Is Nothing Then
                    '--有数据
                    If FBInf.FItem4 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--项目需要新增
                    FCinfo = New FBaseProduct With {.id = FBInf.FItem4, .FName = FBInf.FItemName4, .FPrice = FBInf.FPrice4, .FType = 1, .FUnit = FBInf.FUnit4}
                    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBInf.FItem4 = FCinfo.id
                End If
                '-第五行
                If Not FBInf.FItemName5 Is Nothing Then
                    '--有数据
                    If FBInf.FItem5 = 0 Then
                        '--无数据
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--项目需要新增
                    FCinfo = New FBaseProduct With {.id = FBInf.FItem5, .FName = FBInf.FItemName5, .FPrice = FBInf.FPrice5, .FType = 1, .FUnit = FBInf.FUnit5}
                    If _BILL_Product_ManageBase(FSession, FCinfo, ErrString) = False Then
                        Return False
                    End If
                    If FCinfo.id = Nothing Then
                        ErrString = "新增项目失败!"
                        Return False
                    End If
                    FBInf.FItem5 = FCinfo.id
                End If

                '--组合单据号
                Dim FBLSH As Integer = 0
                If FBInf.FBillNo Is Nothing Then
                    If GetBillMaxNumber(3, FUinf.FOrgid, FBLSH, ErrString) = False Then
                        ErrString = String.Format("取单据号失败!{0}错误信息:{1}", vbCrLf, ErrString)
                        Return False
                    End If
                    FBInf.FBillNo = FBInf.FDate.ToString("yyyyMMdd") & FBLSH.ToString("000000")
                    FBInf.FVerifyCode = Int(FBInf.FAmount * CDbl(FBLSH.ToString("0000")) / CDbl(FBInf.FDate.ToString("yyyyMMdd")) * 987654)
                End If
                If FBInf.FBCode Is Nothing Then
                    FBInf.FBCode = Guid.NewGuid.ToString.ToUpper
                End If

                '--单据条目数
                Dim FItem As String = String.Empty
                Dim FItemV As String = String.Empty

                Dim FQty As String = String.Empty
                Dim FQtyV As String = String.Empty

                Dim FPrice As String = String.Empty
                Dim FPriceV As String = String.Empty

                Dim FUnit As String = String.Empty
                Dim FUnitV As String = String.Empty

                Dim FAcc As String = String.Empty
                Dim FAccV As String = String.Empty

                Dim FItemName As String = String.Empty
                Dim FItemNameV As String = String.Empty

                Dim FMark As String = String.Empty
                Dim FMarkV As String = String.Empty


                '-第一条
                If FBInf.FItem1 > 0 Then
                    FItem += ",FItem1"
                    FItemV += String.Format(",'{0}'", FBInf.FItem1)
                    FUnit += ",FUnit1"
                    FUnitV += String.Format(",'{0}'", FBInf.FUnit1)
                    FQty += ",FQty1"
                    FQtyV += String.Format(",'{0}'", FBInf.FQty1)
                    FPrice += ",FPrice1"
                    FPriceV += String.Format(",'{0}'", FBInf.FPrice1)
                    FAcc += ",FAcc1"
                    FAccV += String.Format(",'{0}'", FBInf.FAcc1)
                    FItemName += ",FItemName1"
                    FItemNameV += String.Format(",'{0}'", FBInf.FItemName1)
                    FMark += ",FMark1"
                    FMarkV += String.Format(",'{0}'", FBInf.FMark1)
                End If

                '-第二条
                If FBInf.FItem2 > 0 Then
                    FItem += ",FItem2"
                    FItemV += String.Format(",'{0}'", FBInf.FItem2)
                    FUnit += ",FUnit2"
                    FUnitV += String.Format(",'{0}'", FBInf.FUnit2)
                    FQty += ",FQty2"
                    FQtyV += String.Format(",'{0}'", FBInf.FQty2)
                    FPrice += ",FPrice2"
                    FPriceV += String.Format(",'{0}'", FBInf.FPrice2)
                    FAcc += ",FAcc2"
                    FAccV += String.Format(",'{0}'", FBInf.FAcc2)
                    FItemName += ",FItemName2"
                    FItemNameV += String.Format(",'{0}'", FBInf.FItemName2)
                    FMark += ",FMark2"
                    FMarkV += String.Format(",'{0}'", FBInf.FMark2)
                End If

                '-第三条
                If FBInf.FItem3 > 0 Then
                    FItem += ",FItem3"
                    FItemV += String.Format(",'{0}'", FBInf.FItem3)
                    FUnit += ",FUnit3"
                    FUnitV += String.Format(",'{0}'", FBInf.FUnit3)
                    FQty += ",FQty3"
                    FQtyV += String.Format(",'{0}'", FBInf.FQty3)
                    FPrice += ",FPrice3"
                    FPriceV += String.Format(",'{0}'", FBInf.FPrice3)
                    FAcc += ",FAcc3"
                    FAccV += String.Format(",'{0}'", FBInf.FAcc3)
                    FItemName += ",FItemName3"
                    FItemNameV += String.Format(",'{0}'", FBInf.FItemName3)
                    FMark += ",FMark3"
                    FMarkV += String.Format(",'{0}'", FBInf.FMark3)
                End If

                '-第四条
                If FBInf.FItem4 > 0 Then
                    FItem += ",FItem4"
                    FItemV += String.Format(",'{0}'", FBInf.FItem4)
                    FUnit += ",FUnit4"
                    FUnitV += String.Format(",'{0}'", FBInf.FUnit4)
                    FQty += ",FQty4"
                    FQtyV += String.Format(",'{0}'", FBInf.FQty4)
                    FPrice += ",FPrice4"
                    FPriceV += String.Format(",'{0}'", FBInf.FPrice4)
                    FAcc += ",FAcc4"
                    FAccV += String.Format(",'{0}'", FBInf.FAcc4)
                    FItemName += ",FItemName4"
                    FItemNameV += String.Format(",'{0}'", FBInf.FItemName4)
                    FMark += ",FMark4"
                    FMarkV += String.Format(",'{0}'", FBInf.FMark4)
                End If

                '-第五条
                If FBInf.FItem5 > 0 Then
                    FItem += ",FItem5"
                    FItemV += String.Format(",'{0}'", FBInf.FItem5)
                    FUnit += ",FUnit5"
                    FUnitV += String.Format(",'{0}'", FBInf.FUnit5)
                    FQty += ",FQty5"
                    FQtyV += String.Format(",'{0}'", FBInf.FQty5)
                    FPrice += ",FPrice5"
                    FPriceV += String.Format(",'{0}'", FBInf.FPrice5)
                    FAcc += ",FAcc5"
                    FAccV += String.Format(",'{0}'", FBInf.FAcc5)
                    FItemName += ",FItemName5"
                    FItemNameV += String.Format(",'{0}'", FBInf.FItemName5)
                    FMark += ",FMark5"
                    FMarkV += String.Format(",'{0}'", FBInf.FMark5)
                End If
                '--组合语句
                sqlstr = String.Format("insert into Bill_Product (FCid,FName,FCName,FBillNo,FVerifyCode,FDate,FRDate{6}{7}{8}{9}{10}{11}{12},FAmount,FUid,FUName,FBCode,FOrgid) values('{0}','{1}','{2}','{3}','{4}','{5}',GETDATE(){13}{14}{15}{16}{17}{18}{19},'{20}','{21}','{22}','{23}',{24})", FBInf.FCid, FBInf.FName, FBInf.FCName, FBInf.FBillNo, FBInf.FVerifyCode, FBInf.FDate.ToString, FItem, FUnit, FQty, FPrice, FAcc, FItemName, FMark, FItemV, FUnitV, FQtyV, FPriceV, FAccV, FItemNameV, FMarkV, FBInf.FAmount, FBInf.FUid, FBInf.FUName, FBInf.FBCode, FUinf.FOrgid)

                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = String.Format("保存单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                    Return False
                End If
                '--取单据ID
                sqlstr = String.Format("select id from Bill_Product where FBCode='{0}'", FBInf.FBCode)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = "保存单据成功但未能取得单据内码"
                    Return False
                End If
                FBInf.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            End If
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "销售单_获取单据信息"
    Public Function _BILL_Product_GetBillInfo(ByVal FSession As String, ByVal FBid As String, ByVal FBillNo As String, ByRef FBInfo As FBillProduct, ByRef ErrString As String) As Boolean Implements IService._BILL_Product_GetBillInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FBid.Trim = String.Empty And FBillNo.Trim = String.Empty Then
            ErrString = FControl.GetErrString("取单据信息失败!", "参数不正确")
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Bill_Product where"
            Dim ds As New DataSet
            If FBid.Trim <> String.Empty Then
                '--按ID查询
                sqlstr += " id=" & FBid.Trim
            ElseIf FBillNo.Trim <> String.Empty Then
                '--按单据号查询
            Else
                FControl.GetErrString("取单据信息失败!", "参数不正确!")
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "内部服务器错误!")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "未找到指定项目")
                Return False
            End If
            '--用户信息
            Dim FUInf As New FUserInfo
            If GetUserInfoBySession(FSession, FUInf, ErrString) = False Then
                ErrString = FControl.GetErrString("取单据信息失败!", ErrString)
                Return False
            End If
            '--检查权限
            If ds.Tables(0).Rows(0).Item("FUid") <> FUInf.id Then
                If _User_CheckAuth(FSession, "BILL", "FUBILLVIEW", "") = False Then
                    ErrString = "没有权限!"
                    Return False
                End If
            End If
            '--写数据项
            Dim r As DataRow = ds.Tables(0).Rows(0)
            FBInfo.id = FControl.cvN(r.Item("id"))
            FBInfo.FCid = FControl.cvN(r.Item("FCid"))
            FBInfo.FName = FControl.cvN(r.Item("FName"))
            FBInfo.FCName = FControl.cvN(r.Item("FCName"))
            FBInfo.FBillNo = FControl.cvN(r.Item("FBillNo"))
            FBInfo.FVerifyCode = FControl.cvN(r.Item("FVerifyCode"))
            FBInfo.FDate = FControl.cvN(r.Item("FDate"))
            FBInfo.FRDate = FControl.cvN(r.Item("FRDate"))
            FBInfo.FItem1 = FControl.cvN(r.Item("FItem1"))
            FBInfo.FItemName1 = FControl.cvN(r.Item("FItemName1"))
            FBInfo.FItem2 = FControl.cvN(r.Item("FItem2"))
            FBInfo.FItemName2 = FControl.cvN(r.Item("FItemName2"))
            FBInfo.FItem3 = FControl.cvN(r.Item("FItem3"))
            FBInfo.FItem4 = FControl.cvN(r.Item("FItem4"))
            FBInfo.FItemName3 = FControl.cvN(r.Item("FItemName3"))
            FBInfo.FItemName4 = FControl.cvN(r.Item("FItemName4"))
            FBInfo.FItem5 = FControl.cvN(r.Item("FItem5"))
            FBInfo.FItemName5 = FControl.cvN(r.Item("FItemName5"))
            FBInfo.FQty1 = FControl.cvN(r.Item("FQty1"))
            FBInfo.FQty2 = FControl.cvN(r.Item("FQty2"))
            FBInfo.FQty3 = FControl.cvN(r.Item("FQty3"))
            FBInfo.FQty4 = FControl.cvN(r.Item("FQty4"))
            FBInfo.FQty5 = FControl.cvN(r.Item("FQty5"))
            FBInfo.FPrice1 = FControl.cvN(r.Item("FPrice1"))
            FBInfo.FPrice2 = FControl.cvN(r.Item("FPrice2"))
            FBInfo.FPrice3 = FControl.cvN(r.Item("FPrice3"))
            FBInfo.FPrice4 = FControl.cvN(r.Item("FPrice4"))
            FBInfo.FPrice5 = FControl.cvN(r.Item("FPrice5"))
            FBInfo.FAcc1 = FControl.cvN(r.Item("FAcc1"))
            FBInfo.FAcc2 = FControl.cvN(r.Item("FAcc2"))
            FBInfo.FAcc3 = FControl.cvN(r.Item("FAcc3"))
            FBInfo.FAcc4 = FControl.cvN(r.Item("FAcc4"))
            FBInfo.FAcc5 = FControl.cvN(r.Item("FAcc5"))
            FBInfo.FMark1 = FControl.cvN(r.Item("FMark1"))
            FBInfo.FMark2 = FControl.cvN(r.Item("FMark2"))
            FBInfo.FMark3 = FControl.cvN(r.Item("FMark3"))
            FBInfo.FMark4 = FControl.cvN(r.Item("FMark4"))
            FBInfo.FMark5 = FControl.cvN(r.Item("FMark5"))
            FBInfo.FAmount = FControl.cvN(r.Item("FAmount"))
            FBInfo.FUid = FControl.cvN(r.Item("FUid"))
            FBInfo.FUName = FControl.cvN(r.Item("FUName"))
            FBInfo.FVid = FControl.cvN(r.Item("FVid"))
            FBInfo.FVName = FControl.cvN(r.Item("FVName"))
            FBInfo.FCancel = FControl.cvN(r.Item("FCancel"))
            FBInfo.FPCount = FControl.cvN(r.Item("FPCount"))
            FBInfo.FBCode = FControl.cvN(r.Item("FBCode"))
            FBInfo.FOrgid = FControl.cvN(r.Item("FOrgid"))
            Return True
        Catch ex As Exception
            FControl.GetErrString("取单据信息失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "销售单_增加打印次数"
    Public Function _Bill_Product_AddPrintCount(ByVal FSession As String, ByRef FBInf As FBillProduct, ByRef ErrString As String) As Boolean Implements IService._Bill_Product_AddPrintCount
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBInf Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBInf.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = "update Bill_Product set FPCount=FPCount+1 output inserted.FPCount where FCancel=0 and id=" & FBInf.id
            Dim ds As New DataSet
            If FBInf.FVid <> FUinf.id Then
                '--打印其他人审核单据,检查权限
                ErrString = "只能打印自己收费的单据"
                Return False
            Else
                '--打印自己审核的单据
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败", "无法找到相应单据")
                Return False
            End If
            FBInf.FPCount = ds.Tables(0).Rows(0).Item("FPCount")
            Return True
        Catch ex As Exception
            FControl.GetErrString("打印失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "销售单_查询"
    Public Function _BILL_Product_GetBillList(ByVal FSession As String, ByVal FSDate As Date, ByVal FEDate As Date, ByVal FName As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Product_GetBillList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ConFUid As String = " and FUid="
            Dim ConFName As String = " and FName like"
            '--检查权限
            Dim FIsAuth As Boolean = _User_CheckAuth(FSession, "BILL", "FUBILLVIEW", "")
            If FIsAuth Then
                '--所有
                ConFUid = Nothing
            Else
                '--自己
                ConFUid += FUinf.id.ToString
            End If
            '--检查单位名
            If Not FName Is Nothing Then
                If FName.Trim <> String.Empty Then
                    ConFName = String.Format(" and FName like '%{0}'", FName.Trim)
                Else
                    ConFName = Nothing
                End If
            Else
                ConFName = Nothing
            End If
            '--制单人单位
            Dim FOrg As String = Nothing
            If FUinf.id = 1 Then
                FOrg = ""
            Else
                FOrg = " and FOrgid=" & FUinf.FOrgid
            End If
            DControl.ExecSQL("update t set t.FOrgName=t1.FName from Bill_Product t,Base_Org t1 where t.FOrgid=t1.id", "")
            Select Case FStyle
                Case "*"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单号',FVerifyCode '验证码',FDate '日期',FItemName1 '项目1名称',FQty1 '数量1',FPrice1 '单价1',FAcc1 '金额1',FMark1 '备注1',FItemName2 '项目2名称',FQty2 '数量2',FPrice2 '单价2',FAcc2 '金额2',FMark2 '备注2',FItemName3 '项目3名称',FQty3 '数量3',FPrice3 '单价3',FAcc3 '金额3',FMark3 '备注3',FItemName4 '项目4名称',FQty4 '数量4',FPrice4 '单价4',FAcc4 '金额4',FMark4 '备注4',FItemName5 '项目5名称',FQty5 '数量5',FPrice5 '单价5',FAcc5 '金额5',FMark5 '备注5',FAmount '总金额',FUName '制单',FVName '收费',FPCount '打印次数',Case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Product where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "*ZF"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单号',FVerifyCode '验证码',FDate '日期',FItemName1 '项目1名称',FQty1 '数量1',FPrice1 '单价1',FAcc1 '金额1',FMark1 '备注1',FItemName2 '项目2名称',FQty2 '数量2',FPrice2 '单价2',FAcc2 '金额2',FMark2 '备注2',FItemName3 '项目3名称',FQty3 '数量3',FPrice3 '单价3',FAcc3 '金额3',FMark3 '备注3',FItemName4 '项目4名称',FQty4 '数量4',FPrice4 '单价4',FAcc4 '金额4',FMark4 '备注4',FItemName5 '项目5名称',FQty5 '数量5',FPrice5 '单价5',FAcc5 '金额5',FMark5 '备注5',FAmount '总金额',FUName '制单',FVName '收费',FPCount '打印次数',Case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Product  where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CN"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Product where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CNZF"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Product  where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            '--查询 
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("查询单据失败!", ErrString)
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("查询时出现错误!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "销售单_修改"
    Public Function _BILL_Product_ModifyBill(ByVal FSession As String, ByRef FBinf As FBillProduct, ByRef ErrString As String) As Boolean Implements IService._BILL_Product_ModifyBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBinf.id <= 0 Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBinf.FVid > 0 Then
                ErrString = "不能修改已收费的单据!"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            If FBinf.FUid <> FUinf.id Then
                ErrString = "只能修改自己填制的单据!"
                Return False
            End If
            '--修改单据
            '-计算验证码
            Dim FBLSH As String = Right(FBinf.FBillNo, 4)
            FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
            '--单据条目数
            Dim FItem As String = String.Empty
            Dim FQty As String = String.Empty
            Dim FPrice As String = String.Empty
            Dim FUnit As String = String.Empty
            Dim FAcc As String = String.Empty
            Dim FItemName As String = String.Empty
            Dim FMark As String = String.Empty
            '-第一条
            If FBinf.FItem1 > 0 Then
                FItem += ",FItem1=" & FBinf.FItem1
                FUnit += String.Format(",FUnit1='{0}'", FBinf.FUnit1)
                FQty += ",FQty1=" & FBinf.FQty1
                FPrice += ",FPrice1=" & FBinf.FPrice1
                FAcc += ",FAcc1=" & FBinf.FAcc1
                FItemName += String.Format(",FItemName1='{0}'", FBinf.FItemName1)
                FMark += String.Format(",FMark1='{0}'", FBinf.FMark1)
            End If
            '-第二条
            If FBinf.FItem2 > 0 Then
                FItem += ",FItem2=" & FBinf.FItem2
                FUnit += String.Format(",FUnit2='{0}'", FBinf.FUnit2)
                FQty += ",FQty2=" & FBinf.FQty2
                FPrice += ",FPrice2=" & FBinf.FPrice2
                FAcc += ",FAcc2=" & FBinf.FAcc2
                FItemName += String.Format(",FItemName2='{0}'", FBinf.FItemName2)
                FMark += String.Format(",FMark2='{0}'", FBinf.FMark2)
            End If

            '-第三条
            If FBinf.FItem3 > 0 Then
                FItem += ",FItem3=" & FBinf.FItem3
                FUnit += String.Format(",FUnit3='{0}'", FBinf.FUnit3)
                FQty += ",FQty3=" & FBinf.FQty3
                FPrice += ",FPrice3=" & FBinf.FPrice3
                FAcc += ",FAcc3=" & FBinf.FAcc3
                FItemName += String.Format(",FItemName3='{0}'", FBinf.FItemName3)
                FMark += String.Format(",FMark3='{0}'", FBinf.FMark3)
            End If
            '-第四条
            If FBinf.FItem4 > 0 Then
                FItem += ",FItem4=" & FBinf.FItem4
                FUnit += String.Format(",FUnit4='{0}'", FBinf.FUnit4)
                FQty += ",FQty4=" & FBinf.FQty4
                FPrice += ",FPrice4=" & FBinf.FPrice4
                FAcc += ",FAcc4=" & FBinf.FAcc4
                FItemName += String.Format(",FItemName4='{0}'", FBinf.FItemName4)
                FMark += String.Format(",FMark4='{0}'", FBinf.FMark4)
            End If

            Dim sqlstr As String = String.Format("update Bill_Product set FVerifyCode='{0}',FAmount='{1}'{2}{3}{4}{5}{6}{7}{8} where id={9}", FBinf.FVerifyCode, FBinf.FAmount, FItem, FUnit, FQty, FPrice, FAcc, FItemName, FMark, FBinf.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                Return False
            End If
            DControl.WriteSysLog("BILL", FUinf.id, FUinf.FName, "修改单据", "单据号:" & FBinf.FBillNo, 0)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "销售单_作废"
    Public Function _Bill_Product_BillCancel(ByVal FSession As String, ByVal FBid As Integer, ByRef ErrString As String) As Boolean Implements IService._Bill_Product_BillCancel
        If FSession Is Nothing Then
            ErrString = "验证用户失败!"
            Return False
        End If
        '--验证用户
        If DControl._GetUserBySession(FSession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        If FBid = Nothing Then
            ErrString = "参数不正确!"
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Bill_Product where id=" & FBid
            Dim FBillNo As String = String.Empty
            Dim FUInf As New FUserInfo
            Dim ds As New DataSet
            If GetUserInfoBySession(FSession, FUInf, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:取单据信息时出错 {1}", vbCrLf, ErrString)
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = String.Format("作废单据失败!{0}错误信息:内部服务器错误", vbCrLf)
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = String.Format("作废单据失败!{0}错误信息:无法找到指定的记录", vbCrLf)
                Return False
            End If
            FBillNo = ds.Tables(0).Rows(0).Item("FBillNo")
            If ds.Tables(0).Rows(0).Item("FVid") <> Nothing And FUInf.id <> 1 Then
                ErrString = "只有管理员可以作废已收费的单据!"
                Return False
            End If
            '--检查权限
            If ds.Tables(0).Rows(0).Item("FUid") = FUInf.id Then
                '--自己的单据
                If _User_CheckAuth(FSession, "BILL", "FUBILLCANCEL", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            Else
                '--其它人的单据
                If _User_CheckAuth(FSession, "BILL", "FUBILLCANCELA", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            End If
            '--开始作废
            sqlstr = "update Bill_Product set FCancel=-1 where id=" & FBid
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            '--写日志
            DControl.WriteSysLog("BILL", FUInf.id, FUInf.FRName, "作废单据", "作废商品销售收据 单号:" & FBillNo, 1)
            Return True
        Catch ex As Exception
            ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End Try
    End Function
#End Region
   
End Class
